﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.VisualBasic;
using System.Windows.Forms;
namespace GodKA
{
    public class IniOperation
    {
        public  string inifilename = "";
        public IniOperation(string filename)
        {
            inifilename = filename;
        }
        /// <summary>
        /// 写入INI
        /// </summary>
        /// <param name="section">INI文件中的段落名称</param>
        /// <param name="key">INI文件中的关键字</param>
        /// <param name="val">写入的字符串</param>
        /// <param name="filePath">INI文件的完整路径和名称。</param>
        /// <returns></returns>
        [DllImport("kernel32")]
        public static extern long WritePrivateProfileString(string section, string key,
                                                             string val, string filePath);


        /*参数说明：section：INI文件中的段落；key：INI文件中的关键字；
          val：INI文件中关键字的数值；filePath：INI文件的完整的路径和名称。*/
        /// <summary>
        /// 读取INI文件
        /// </summary>
        /// <param name="section">INI文件中的段落名称</param>
        /// <param name="key">INI文件中的关键字</param>
        /// <param name="def">无法读取时候时候的缺省数值</param>
        /// <param name="retVal">读取返回字符串</param>
        /// <param name="size">数值的大小</param>
        /// <param name="filePath">INI文件的完整路径和名称。</param>
        /// <returns></returns>
        [DllImport("kernel32")]
        public static extern int GetPrivateProfileString(string section, string key,
                                                          string def, System.Text.StringBuilder retVal,
                                                          int size, string filePath);

        /// <summary>
        /// 键盘映射
        /// </summary>
        /// <param name="vKey">键盘256个虚拟映射</param>
        /// <returns>映射的值理论上是-32768</returns>
        [DllImport("User32"),]
        public static extern int GetAsyncKeyState(long vKey);

        public  string ReadIniString(string section, string key)//, string def, int size, string filePath)
        {
            int size = 65535;
            string def = null;
            StringBuilder str = new StringBuilder(size);
            GetPrivateProfileString(section, key, def, str, size, inifilename);
            return str.ToString();
        }

        public  int ReadIniInt(string section, string key)//, string def, int size, string filePath)
        {
            int size = 65535;
            string def = null;
            StringBuilder str = new StringBuilder(size);
            GetPrivateProfileString(section, key, def, str, size, inifilename);
            string result = str.ToString();
            result = result.Split(";".ToCharArray(), 2)[0];
            return int.Parse(result);
        }
        public  void PutIniString(string section, string key, string val)
        {
            WritePrivateProfileString(section, key, val, inifilename);
        }
    }
    public static class Str
    {
        public static string Big5ToUnicode(byte[] big5bytes)
        {
            return System.Text.Encoding.GetEncoding("BIG5").GetString(big5bytes).Replace("\0","");
        }

        public static byte[] UnicodeToBig5(string str, int length)
        {
            string st = Strings.StrConv(str.Replace(" ", ""), VbStrConv.TraditionalChinese, 0);
            byte[] big5bytes = System.Text.Encoding.GetEncoding("BIG5").GetBytes(st);
            if (length <= 0) return big5bytes;
            else
            {
                byte[] result = new byte[length];
                for (int i = 0; i < Math.Min(big5bytes.Length, length); i++)
                {
                    result[i] = big5bytes[i];
                }
                return result;
            }

        }

        public static byte[] UnicodeToBig5(string str)
        {
            return UnicodeToBig5(str, 0);
        }

        public static string GBKToUnicode(byte[] GBKbytes)
        {
            return System.Text.Encoding.GetEncoding("GBK").GetString(GBKbytes);
        }

        public static byte[] UnicodeToGBK(string str)
        {
            return UnicodeToGBK(str, 0);
        }

        public static byte[] UnicodeToGBK(string str, int length)
        {
            string st = Strings.StrConv(str.Replace(" ", ""), VbStrConv.TraditionalChinese, 0);
            byte[] GBKbytes = System.Text.Encoding.GetEncoding("GBK").GetBytes(st);
            if (length <= 0) return GBKbytes;
            else
            {
                byte[] result = new byte[length];
                for (int i = 0; i < Math.Min(GBKbytes.Length, length); i++)
                {
                    result[i] = GBKbytes[i];
                }
                return result;
            }

        }

        public static string SimpleToTradition(string str)
        {
            return Strings.StrConv(str.Replace(" ", ""), VbStrConv.TraditionalChinese, 0);
        }

        public static string TraditionToSimple(string str)
        {
            return Strings.StrConv(str.Replace(" ", ""), VbStrConv.SimplifiedChinese, 0);
        }

        public static int GetStringLength(string str)
        {
            string st = Strings.StrConv(str.Replace(" ", ""), VbStrConv.TraditionalChinese, 0);
            byte[] big5bytes = System.Text.Encoding.GetEncoding("BIG5").GetBytes(st);

            int n = 0;
            for (int i = 0; i < big5bytes.Length; i++)
            {
                if (big5bytes[i] > 0) n++;
                else break;
            }
            return n;
        }

        public static string InputBox(string Caption, string Hint, string Default)
        {
            Form InputForm = new Form();
            InputForm.MinimizeBox = false;
            InputForm.MaximizeBox = false;
            InputForm.StartPosition = FormStartPosition.CenterScreen;
            InputForm.Width = 220;
            InputForm.Height = 150;
            InputForm.FormBorderStyle = FormBorderStyle.FixedDialog;
            InputForm.Text = Caption;
            Label lbl = new Label();
            lbl.Text = Hint;
            lbl.Left = 10;
            lbl.Top = 20;
            lbl.Parent = InputForm;
            lbl.AutoSize = true;
            TextBox tb = new TextBox();
            tb.Left = 30;
            tb.Top = 45;
            tb.Width = 160;
            tb.Parent = InputForm;
            tb.Text = Default;
            tb.SelectAll();
            Button btnok = new Button();
            btnok.Left = 30;
            btnok.Top = 80;
            btnok.Parent = InputForm;
            btnok.Text = "确定";
            InputForm.AcceptButton = btnok;//回车响应

            btnok.DialogResult = DialogResult.OK;
            Button btncancal = new Button();
            btncancal.Left = 120;
            btncancal.Top = 80;
            btncancal.Parent = InputForm;
            btncancal.Text = "取消";
            btncancal.DialogResult = DialogResult.Cancel;
            tb.Text = Default;
            try
            {
                if (InputForm.ShowDialog() == DialogResult.OK)
                {
                    return tb.Text;
                }
                else
                {
                    return null;
                }
            }
            finally
            {
                InputForm.Dispose();
            }
        }

    }
}
